package com.ruoyi.project.xcx.myhome.goods.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.project.xcx.myhome.category.domain.XcxMyhomeCategory;
import com.ruoyi.project.xcx.myhome.category.mapper.XcxMyhomeCategoryMapper;
import com.ruoyi.project.xcx.myhome.category.service.IXcxMyhomeCategoryService;
import com.ruoyi.project.xcx.myhome.goods.dto.GoodsDto;
import com.ruoyi.project.xcx.user.domain.XcxUser;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.xcx.myhome.goods.mapper.XcxMyhomeGoodsMapper;
import com.ruoyi.project.xcx.myhome.goods.domain.XcxMyhomeGoods;
import com.ruoyi.project.xcx.myhome.goods.service.IXcxMyhomeGoodsService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * myhome物品Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-10
 */
@Service
public class XcxMyhomeGoodsServiceImpl implements IXcxMyhomeGoodsService 
{
    @Autowired
    private XcxMyhomeGoodsMapper xcxMyhomeGoodsMapper;
    @Autowired
    private XcxMyhomeCategoryMapper xcxMyhomeCategoryMapper;
    @Autowired
    private IXcxMyhomeCategoryService xcxMyhomeCategoryService;

    @Override
    @Transactional
    public void addGoods(GoodsDto goods, XcxUser user) {
        // 判断类别是否存在，不存在新增
        XcxMyhomeCategory categoryQuery = new XcxMyhomeCategory();
        categoryQuery.setUserId(user.getId());
        categoryQuery.setName(goods.getCategoryName());
        List<XcxMyhomeCategory> categoryList = xcxMyhomeCategoryMapper.selectCategoryByName(categoryQuery);
        Long categoryId = null;
        if(categoryList.isEmpty()){
            // 新增
            categoryQuery = new XcxMyhomeCategory();
            categoryQuery.setName(goods.getCategoryName());
            categoryQuery.setUserId(user.getId());
            categoryQuery.setCount(1L);
            xcxMyhomeCategoryMapper.insertXcxMyhomeCategory(categoryQuery);
            categoryId = categoryQuery.getId();
        }else{
            categoryId = categoryList.get(0).getId();
            // 查询本分类下面物品是否已经存在
            XcxMyhomeGoods goodsQuery = new XcxMyhomeGoods();
            goodsQuery.setUserId(user.getId());
            goodsQuery.setCategoryId(categoryId);
            goodsQuery.setName(goods.getName());
            List<XcxMyhomeGoods> goodsList = xcxMyhomeGoodsMapper.selectGoodsByName(goodsQuery);
            if(!goodsList.isEmpty()){
                throw new BusinessException(goods.getCategoryName() + "分类下该物品已经存在");
            }
        }
        XcxMyhomeGoods addGoods = new XcxMyhomeGoods();
        BeanUtils.copyProperties(goods, addGoods);
        addGoods.setUserId(user.getId());
        addGoods.setCategoryId(categoryId);
        addGoods.setInsertDate(new Date());
        xcxMyhomeGoodsMapper.insertXcxMyhomeGoods(addGoods);
    }

    @Override
    @Transactional
    public void editGoods(GoodsDto goods, XcxUser user) {
        XcxMyhomeGoods goodsOld = xcxMyhomeGoodsMapper.selectXcxMyhomeGoodsById(goods.getId());
        if(goodsOld == null || !goodsOld.getUserId().equals(user.getId())){
            throw new BusinessException("修改的物品不存在");
        }
        Long categoryId = null;
        XcxMyhomeCategory categoryOld = xcxMyhomeCategoryMapper.selectXcxMyhomeCategoryById(goodsOld.getCategoryId());
        if(!goods.getCategoryName().equals(categoryOld.getName())){
            // 修改物品的分类和原来的分类相同，那么说明物品的类别没有改变，否则物品类别改变。
            // 物品类别改变，那么原来物品类别个数-1。
            xcxMyhomeCategoryMapper.updateCategoryCount(-1, categoryOld.getId());
            // 判断新的物品类别是否存在，不存在新增类别。已存在类别把数量+1
            XcxMyhomeCategory categoryQuery = new XcxMyhomeCategory();
            categoryQuery.setUserId(user.getId());
            categoryQuery.setName(goods.getCategoryName());
            List<XcxMyhomeCategory> categoryList = xcxMyhomeCategoryMapper.selectCategoryByName(categoryQuery);
            if(categoryList.isEmpty()){
                // 新增
                categoryQuery = new XcxMyhomeCategory();
                categoryQuery.setName(goods.getCategoryName());
                categoryQuery.setUserId(user.getId());
                categoryQuery.setCount(1L);
                xcxMyhomeCategoryMapper.insertXcxMyhomeCategory(categoryQuery);
                categoryId = categoryQuery.getId();
            }else{
                categoryId = categoryList.get(0).getId();
                // 查询本分类下面物品是否已经存在
                XcxMyhomeGoods goodsQuery = new XcxMyhomeGoods();
                goodsQuery.setUserId(user.getId());
                goodsQuery.setCategoryId(categoryId);
                goodsQuery.setName(goods.getName());
                List<XcxMyhomeGoods> goodsList = xcxMyhomeGoodsMapper.selectGoodsByName(goodsQuery);
                if(!goodsList.isEmpty()){
                    throw new BusinessException(goods.getCategoryName() + "分类下该物品已经存在，不能修改物品到该分类");
                }
                // 物品数量新增1
                xcxMyhomeCategoryMapper.updateCategoryCount(1, categoryId);
            }
        }else{
            categoryId = categoryOld.getId();
        }
        XcxMyhomeGoods upGoods = new XcxMyhomeGoods();
        BeanUtils.copyProperties(goods, upGoods);
        upGoods.setUserId(user.getId());
        upGoods.setCategoryId(categoryId);
        upGoods.setUpdateDate(new Date());
        xcxMyhomeGoodsMapper.updateXcxMyhomeGoods(upGoods);
    }

    @Override
    public void deleteGoods(Long id, XcxUser user) {
        XcxMyhomeGoods goods = xcxMyhomeGoodsMapper.selectXcxMyhomeGoodsById(id);
        if(goods == null || !goods.getUserId().equals(user.getId())){
            throw new BusinessException("物品不存在");
        }
        // 物品类别数量减一
        xcxMyhomeCategoryMapper.updateCategoryCount(-1, goods.getCategoryId());
        // 物品删除
        xcxMyhomeGoodsMapper.deleteXcxMyhomeGoodsById(id);
    }

    @Override
    public void decreaseGoods(Long id, XcxUser user) {
        xcxMyhomeGoodsMapper.decreaseGoods(id, user.getId());
    }

    @Override
    public List<GoodsDto> listGoods(GoodsDto goods, XcxUser user) {
        // 有id按照id进行查询
        // 没有id判断类型
            // 类型是物品，那么查询物品，匹配类别名称
            // 类型是分类，那么查询分类，查分类下面的所有物品
        // todo

        XcxMyhomeGoods queryGoods = new XcxMyhomeGoods();
        queryGoods.setUserId(user.getId());
        queryGoods.setCategoryId(goods.getCategoryId());
        queryGoods.setName(goods.getName());
        Map<Long, String> categoryNames = xcxMyhomeCategoryService.queryAllCategoryName(user.getId());
        List<XcxMyhomeGoods> goodsList = xcxMyhomeGoodsMapper.selectXcxMyhomeGoodsList(queryGoods);
        List<GoodsDto> goodsDtoList = new ArrayList<>(goodsList.size());
        GoodsDto goodsDto;
        for(XcxMyhomeGoods temp:goodsList){
            goodsDto = new GoodsDto();
            BeanUtils.copyProperties(temp, goodsDto);
            goodsDto.setCategoryName(categoryNames.get(temp.getCategoryId()));
            goodsDtoList.add(goodsDto);
        }
        goodsDtoList.stream().sorted(new Comparator<GoodsDto>() {
            @Override
            public int compare(GoodsDto o1, GoodsDto o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        return goodsDtoList;
    }

    @Override
    public GoodsDto getGoods(Long id, XcxUser user) {
        XcxMyhomeGoods goods = selectXcxMyhomeGoodsById(id);
        if(goods == null){
            return null;
        }
        XcxMyhomeCategory category = xcxMyhomeCategoryMapper.selectXcxMyhomeCategoryById(goods.getCategoryId());
        GoodsDto goodsDto = new GoodsDto();
        BeanUtils.copyProperties(goods, goodsDto);
        goodsDto.setCategoryName(category.getName());
        return goodsDto;
    }

    /**
     * 查询myhome物品
     * 
     * @param id myhome物品ID
     * @return myhome物品
     */
    @Override
    public XcxMyhomeGoods selectXcxMyhomeGoodsById(Long id)
    {
        return xcxMyhomeGoodsMapper.selectXcxMyhomeGoodsById(id);
    }

    /**
     * 查询myhome物品列表
     * 
     * @param xcxMyhomeGoods myhome物品
     * @return myhome物品
     */
    @Override
    public List<XcxMyhomeGoods> selectXcxMyhomeGoodsList(XcxMyhomeGoods xcxMyhomeGoods)
    {
        return xcxMyhomeGoodsMapper.selectXcxMyhomeGoodsList(xcxMyhomeGoods);
    }

    /**
     * 新增myhome物品
     * 
     * @param xcxMyhomeGoods myhome物品
     * @return 结果
     */
    @Override
    public int insertXcxMyhomeGoods(XcxMyhomeGoods xcxMyhomeGoods)
    {
        return xcxMyhomeGoodsMapper.insertXcxMyhomeGoods(xcxMyhomeGoods);
    }

    /**
     * 修改myhome物品
     * 
     * @param xcxMyhomeGoods myhome物品
     * @return 结果
     */
    @Override
    public int updateXcxMyhomeGoods(XcxMyhomeGoods xcxMyhomeGoods)
    {
        return xcxMyhomeGoodsMapper.updateXcxMyhomeGoods(xcxMyhomeGoods);
    }

    /**
     * 删除myhome物品对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteXcxMyhomeGoodsByIds(String ids)
    {
        return xcxMyhomeGoodsMapper.deleteXcxMyhomeGoodsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除myhome物品信息
     * 
     * @param id myhome物品ID
     * @return 结果
     */
    @Override
    public int deleteXcxMyhomeGoodsById(Long id)
    {
        return xcxMyhomeGoodsMapper.deleteXcxMyhomeGoodsById(id);
    }
}
